<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="reviewReqRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="评审时间" prop="rtime">
            <el-date-picker
              v-model="form.rtime"
              type="datetime"
              placeholder="请选择评审时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="评审意见" prop="review_result">
            <y-select
              v-model="form.review_result"
              placeholder="请选择评审意见"
              :options="pms_req_review"
              @change="changeReviewResultOptions"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="ownerVisible">
        <el-col :span="12">
          <el-form-item label="负责人" prop="owner">
            <y-select
              v-model="form.owner"
              placeholder="请选择负责人"
              :optionsFunc="listUser"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="priorityVisible">
        <el-col :span="12">
          <el-form-item label="优先级" prop="priority">
            <y-select
              v-model="form.priority"
              placeholder="请选择优先级"
              :options="sys_priority_level"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="closeVisible">
        <el-col :span="12">
          <el-form-item label="拒绝原因" prop="closed_type">
            <y-select
              v-model="form.closed_type"
              placeholder="请选择拒绝原因"
              :options="pms_req_close"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="review_remark">
            <el-input
              v-model="form.review_remark"
              type="textarea"
              :rows="5"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="ReqReview">
import { listUser } from "@/api/sys/user";

const { proxy } = getCurrentInstance();
const { pms_req_review, sys_priority_level, pms_req_close } = proxy.useDict(
  "pms_req_review",
  "sys_priority_level",
  "pms_req_close"
);

const emit = defineEmits(["submited", "close"]);

const ownerVisible = ref(false);
const priorityVisible = ref(false);
const closeVisible = ref(false);

const data = reactive({
  form: {},
  rules: {
    rtime: [
      { required: true, message: "需求评审时间不能为空", trigger: "blur" },
    ],
    comment: [
      { required: true, message: "需求评审意见不能为空", trigger: "blur" },
    ],
  },
});

const { form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: "" },
  getMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 打开对话框事件 */
function open() {
  props.getMethod(props.id).then((res) => {
    form.value = res.data;
    form.value.rtime = new Date();
    changeReviewResultOptions(form.value.review_result);
  });
}

/** 评审结果选择事件*/
function changeReviewResultOptions(val) {
  switch (val) {
    case "1": //有待明确
      form.value.status = "draft"; // 将需求状态置为 “草稿”
      ownerVisible.value = true;
      priorityVisible.value = false;
      closeVisible.value = false;
      break;
    case "2": //拒绝
      form.value.status = "closed"; // 将需求状态置为 “已关闭”
      ownerVisible.value = false;
      priorityVisible.value = false;
      closeVisible.value = true;
      break;
    default: //确认通过
      form.value.status = "activated"; // 将需求状态置为 “激活”
      ownerVisible.value = true;
      priorityVisible.value = true;
      closeVisible.value = false;
      break;
  }
}

/** 提交按钮事件 */
function submit() {
  proxy.$refs["reviewReqRef"].validate((valid) => {
    if (valid) {
      props.updateMethod(form.value).then((res) => {
        proxy.$modal.msgSuccess("评审成功");
        submited();
      });
    }
  });
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
